<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
          :model="where"
          label-width="77px"
          class="ele-form-search"
          @keyup.enter.native="reload"
          @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="4" :md="8">
            <el-form-item label="所属单位:">
              <el-select
                  clearable
                  class="ele-block"
                  v-model="where.unit_id"
                  placeholder="所属单位">
                <el-option
                    v-for="item in unit"
                    :key="item.id"
                    :label="item.unit_name"
                    :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="8">
            <el-form-item label="分类名称:">
              <el-input
                  clearable
                  v-model="where.name"
                  placeholder="请输入巡查分类名称"/>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="8">
            <el-form-item label="异常等级:">
              <el-select
                  clearable
                  class="ele-block"
                  v-model="where.level"
                  placeholder="异常等级">
                <el-option label="一级" :value="1"/>
                <el-option label="二级" :value="2"/>
                <el-option label="三级" :value="3"/>
              </el-select>
            </el-form-item>

          </el-col>
          <el-col :lg="4" :md="8">
            <el-form-item label="状态:">
              <el-select
                  clearable
                  class="ele-block"
                  v-model="where.status"
                  placeholder="状态">
                <el-option label="停用" :value="1"/>
                <el-option label="启用" :value="2"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="8">
            <el-form-item label="所属模块:">
              <el-select
                  clearable
                  class="ele-block"
                  v-model="where.model_type"
                  placeholder="所属模块">
                <el-option label="应急管理" value="emergency"/>
                <el-option label="道路管理" value="way"/>
                <el-option label="环保管理" value="protection"/>
                <el-option label="违建管理" value="illegal"/>
              </el-select>
            </el-form-item>

          </el-col>
          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button
                  type="primary"
                  icon="el-icon-search"
                  class="ele-btn-icon"
                  @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
          ref="table"
          :where="where"
          :datasource="url"
          :columns="columns"
          default-expand-all
          :span-method="arraySpanMethod"
          :row-class-name="tableRowClassName"
          row-key="id"
          :need-page="false"
          :selection.sync="selection"
          height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
<!--          <el-button-->
<!--              size="small"-->
<!--              type="primary"-->
<!--              icon="el-icon-plus"-->
<!--              class="ele-btn-icon"-->
<!--              @click="openEdit(null)"-->
<!--              v-if="permission.includes('sys:abnormal:add')">添加-->
<!--          </el-button>-->
          <el-button
              size="small"
              type="danger"
              icon="el-icon-delete"
              class="ele-btn-icon"
              @click="removeBatch"
              v-if="permission.includes('sys:abnormal:dall')">删除
          </el-button>
        </template>

        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
              type="primary"
              :underline="false"
              icon="el-icon-plus"
              @click="openEdit(null, row)"
              v-if="(row.level < 3 || row.id < 0) && permission.includes('sys:abnormal:add')">添加
          </el-link>
          <template v-if="row.id > 0">
            <el-link
                type="primary"
                :underline="false"
                icon="el-icon-edit"
                @click="openEdit(row)"
                v-if="permission.includes('sys:abnormal:edit')">修改
            </el-link>
            <el-popconfirm
                class="ele-action"
                title="确定要删除此巡查分类吗？"
                @confirm="remove(row)">
              <el-link
                  type="danger"
                  slot="reference"
                  :underline="false"
                  icon="el-icon-delete"
                  v-if="permission.includes('sys:abnormal:delete')">删除
              </el-link>
            </el-popconfirm>
          </template>
          <el-link
              type="primary"
              :underline="false"
              icon="el-icon-magic-stick"
              @click="earlyWarning(row)"
              v-if="permission.includes('sys:abnormal:edit')">预警规则
          </el-link>
        </template>
<!--        <template slot="platform" slot-scope="{row}">-->
<!--          <el-tag v-if="row.model_type === 'emergency'" type="success" size="small">应急管理</el-tag>-->
<!--          <el-tag v-if="row.model_type === 'way'" type="warning" size="small">道路管理</el-tag>-->
<!--          <el-tag v-if="row.model_type === 'protection'" type="danger" size="small">环保管理</el-tag>-->
<!--          <el-tag v-if="row.model_type === 'illegal'" type="info" size="small">违建管理</el-tag>-->
<!--        </template>-->
        <template slot="level" slot-scope="{row}">
          <template v-if="row.id > 0">
            <el-tag v-if="row.level === 1" type="success" size="small">一级</el-tag>
            <el-tag v-if="row.level === 2" type="success" size="small">二级</el-tag>
            <el-tag v-if="row.level === 3" type="success" size="small">三级</el-tag>
          </template>
        </template>
        <!-- 状态列 -->
        <template slot="status" slot-scope="{row}">
          <el-switch
              v-if="row.id > 0"
              v-model="row.status"
              @change="status(row)"
              :active-value="2"
              :inactive-value="1"/>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <abnormal-edit v-if="showEdit" :data="current" :parent="pCurrent" :visible.sync="showEdit" @done="reload"/>
    <early-warning-config v-if="earlyWarningDialog" :data="current" :visible.sync="earlyWarningDialog"></early-warning-config>
  </div>
</template>

<script>
import {mapGetters} from "vuex";
import AbnormalEdit from './abnormal-edit';
import earlyWarningConfig from './early-warning-config';

export default {
  name: 'ExamAbnormal',
  components: {AbnormalEdit, earlyWarningConfig},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/abnormal/tree',
      // 表格列配置
      columns: [
        {
          prop: 'name',
          label: '巡查分类名称',
          showOverflowTooltip: true,
          minWidth: 300,
          align: 'left',
        },
        {
          prop: 'unit_name',
          label: '单位名称',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'level',
          label: '异常等级',
          showOverflowTooltip: true,
          width: 100,
          align: 'center',
          slot: 'level'
        },
        {
          prop: 'status',
          label: '状态',
          align: 'center',
          width: 100,
          resizable: false,
          slot: 'status',
        },
        {
          prop: 'update_time',
          label: '更新时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          width: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 240,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      unit: [],
      // 表格搜索条件
      where: {'all': 1},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      pCurrent: null,
      // 是否显示编辑弹窗
      showEdit: false,
      earlyWarningDialog: false
    };
  },
  created() {
    this.getUnitList()
  },
  methods: {
    async getUnitList() {
      this.$http.get('/unit/index', {gettype: 'all'}).then(res => {
        this.unit = res.data.data
      })
    },
    arraySpanMethod({ row }) {
      if (row.id < 0) {
        //return [1, 5]
      }
    },
    tableRowClassName({row}) {
      if (row.id < 0) {
        return 'module-row'
      }
      return ''
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row, pRow) {
      this.current = row;
      this.pCurrent = pRow;
      this.showEdit = true;
    },
    earlyWarning(row) {
      this.current = row
      this.earlyWarningDialog = true
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/abnormal/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的巡查分类吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/abnormal/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },

    /* 更改状态 */
    status(row) {
      const loading = this.$loading({lock: true});
      let params = Object.assign({
        "id": row.id,
        "status": row.status
      })
      this.$http.post('/abnormal/status', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
        } else {
          row.status = !row.status ? 1 : 2;
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
  }
}
</script>

<style>
.el-table .module-row {
  background-color: #262626;
}
</style>
